<?php
/**
* 用户登录、退出、改密码、模块、日志
*
*
* @author     wangpeng
* @version    1.0
* @package    Base Framework
* @access     public
* @copyright  商业软件,受著作权保护
* @link       http://***
*/
class managerModule extends Base
{
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){}

	/**
	 * 登录验证
	 * 
	 * @param string $username 用户名
	 * @param string $password 密码
	 * 
	 * @return int 添加成功文章ID
	 */
	public function login($sUsername, $sPassword)
	{
		// $sUsername = preg_replace("/[^一-龥]/u","",$sUsername);
		// $sPassword = preg_replace("/([a-z0-9]+)/i", "\\1", $sPassword);
		
		//取数据
		$oDmo = new DMO_Admin_User();
		$oDmo->setField('adminid,username,password,lastip,logincount,groupids,pellets,state');
		$aKey = array("username"=>$sUsername);
		$aR = $oDmo->getRow($aKey);
		
		//用户不存在
		if(!$aR)
		{
			$this->error("用户名或密码错误,请重新输入");
		}
		
		//验证密码
		if($aR['password'] != md5($sPassword))
		{
			$this->error("用户名或密码错误,请重新输入");
		}
		
		//账号已关闭
		if($aR['state'] != '1')
		{
			$this->error("账号已关闭");
		}

		//记录上次登录信息
		$oDto = new DTO_Admin_User();
		$oDto->adminid = $aR['adminid'];
		$oDto->lastip =  Common::getIpSimple();
		$oDto->lastlogin = time();

		$oDto->logincount = $aR['logincount']+1;

		$oDmo->setField('lastip,lastlogin,logincount');
		if($oDmo->modify($oDto) === false)
		{
			$this->error("系统内部错误,请稍候访问");
		}

		//用户组
		$oDmo = new DMO_Admin_Group();
		$oDmo->setField('groupname,acl');
		$aKey = array("+groupid" => $aR['groupids']);
		$aGroup = $oDmo->getAll($aKey);

		$acl_arr = $group_arr = array();
		foreach($aGroup as $item)
		{
			$group_arr[] = $item['groupname'];
			$acl_arr[] = $item['acl'];
		}

		//初始化
		$acl = explode("\n", join("\n", $acl_arr));
		foreach($acl as $key => $val)
		{
			$acl[$key] = rtrim($val, "\r");
		}
		$_SESSION['admin']['acl'] = join(",", $acl);

		//写入登录信息
		$_SESSION['admin']['groupname'] = join(", ", $group_arr);
		$_SESSION['admin']['adminid'] = $aR['adminid'];
		$_SESSION['admin']['username'] = $aR['username'];
		$_SESSION['admin']['lastip'] = $aR['lastip'];
		$_SESSION['admin']['pellets'] = $aR['pellets'] == "AllPellet" ? "AllPellet" : explode(",",$aR['pellets']);

		parent::$admin = $_SESSION['admin'];

		//记录登录日志
		$this->admin_managerModule->addLog(10, "login");
		
		return true;
	}

	/**
	 * 退出登录
	 * 
	 * @param string $username 用户名
	 * @param string $password 密码

	 * @return int 添加成功文章ID
	 */
	public function logout()
	{
		//Common::debug();
		unset($_SESSION['admin']);

		//记录登录日志
		$this->admin_managerModule->addLog(10, "logout");
	}

	/**
	 * 获取密码
	 * 
	 * @param string $uid UID
	 * @return string
	 */
	public function getpass($uid)
	{
		$oDmo = new DMO_Admin_User();
		$oDmo->setField('password');
		$aKey = array("adminid"=>$uid);
		$aR = $oDmo->getRow($aKey);

		return $aR['password'];
	}

	/**
	 * 修改密码
	 * 
	 * @param string $uid UID
	 * @return string
	 */
	public function setpass($uid, $pass)
	{
		$oDto = new DTO_Admin_User();
		$oDto->adminid = $uid;
		$oDto->password = $pass;

		$oDmo = new DMO_Admin_User();
		$oDmo->setField('password');
		if($oDmo->modify($oDto) === false)
		{
			throw new MyException("数据操作错误");
		}

		return true;
	}

	/**
	 * 记录操作日志
	 * 
	 * @param string $subsys 子系统
	 * @param string $opt 操作
	 * @param int $id 信息id
	 * 
	 * @return boolean
	 */
	public function addLog($channel, $opt, $content='')
	{
		$oDto = new DTO_Admin_Log();
		$oDto->adminid = parent::$admin['adminid'];
		$oDto->ip = Common::getIpSimple();
		$oDto->createtime = time();
		$oDto->channel = $channel;
		$oDto->opt = $opt;
		$oDto->ext = $content;

		$oDmo = new DMO_Admin_Log();
		if(!$oDmo->add($oDto))
		{
			throw new MyException("系统错误,请稍候再试");
		}

		return true;
	}

	/**
	 * 列出操作日志
	 * 
	 * @param Object $Opage 分页
	 * 
	 * @return boolean
	 */
	public function listLogs($oPage)
	{
		$aKey = array();
		$aOrder = array("createtime"=>"desc");

		$oDmo = new DMO_Admin_Log();
		$aRes = $oDmo->getPage($oPage->getBegin(), $oPage->PerPage, $aKey, $aOrder);
		$oPage->Count = $oDmo->getTotal();

		if($aRes)
		{
			$aUser = array();
			foreach($aRes as $key => $val)
			{
				$aUser[$val['adminid']] = $val['adminid'];
			}

			$oDmo = new DMO_Admin_User();
			$oDmo->setField("adminid,username");
			$aResUser = $oDmo->getAll(array("+adminid"=>join(",", $aUser)));
			if($aResUser)
			{
				foreach($aResUser as $u)
				{
					$aU[$u['adminid']] = $u['username'];
				}
			}
			$aU[0] = "SYSTEM";
			foreach($aRes as $key => $val)
			{
				$aRes[$key]['adminname'] = $aU[$val['adminid']];
			}
		}

		return $aRes;
	}
}
?>